﻿using Compass.Wasm.Shared.Wms;
using Compass.Wasm.Shared;
using AutoMapper;
using Compass.WmsService.Domain;
using Compass.WmsService.Infrastructure;
using Compass.WmsService.Domain.Entities;

namespace Compass.Wasm.Server.Services.Wms;
public interface ILocationService : IBaseService<LocationDto>
{
    Task<ApiResponse<LocationDto>> GetSingleByNameAsync(string name);
    Task<ApiResponse<IEnumerable<LocationDto>>> GetAllByAreaAsync(Warehouse_e warehouse,Area_e area, string ranks);
}
public class LocationService : ILocationService
{
    #region ctor
    private readonly WmsDbContext _dbContext;
    private readonly IWmsRepository _repository;
    private readonly IMapper _mapper;
    public LocationService(WmsDbContext dbContext, IWmsRepository repository, IMapper mapper)
    {
        _dbContext = dbContext;
        _repository = repository;
        _mapper = mapper;
    } 
    #endregion

    #region 基本增删改查
    public async Task<ApiResponse<IEnumerable<LocationDto>>> GetAllAsync()
    {
        try
        {
            var models = await _repository.GetLocationsAsync();
            //按照库位排序
            var dtos = await _mapper.ProjectTo<LocationDto>(models).OrderBy(x => x.Name).ToListAsync();
            //查询物料信息
            foreach (var dto in dtos)
            {
                var cargo = await _repository.GetCargoByLocationAsync(dto.Name);
                if (cargo==null) continue;
                dto.MtlNumber = cargo.MtlNumber;
                dto.Description = cargo.Description;
            }
            return new ApiResponse<IEnumerable<LocationDto>> { Status = true, Result = dtos };
        }
        catch (Exception e)
        {
            return new ApiResponse<IEnumerable<LocationDto>> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<LocationDto>> GetSingleAsync(Guid id)
    {
        try
        {
            var model = await _repository.GetLocationByIdAsync(id);
            if (model == null) return new ApiResponse<LocationDto> { Status = false, Message = "查询数据失败" };
            var dto = _mapper.Map<LocationDto>(model);
            var cargo = await _repository.GetCargoByLocationAsync(dto.Name);
            if (cargo!=null)
            {
                dto.MtlNumber = cargo.MtlNumber;
                dto.Description = cargo.Description;
            }
            return new ApiResponse<LocationDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<LocationDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<LocationDto>> AddAsync(LocationDto dto)
    {
        try
        {
            var model = new Location(Guid.NewGuid(), dto.Warehouse, dto.Area, dto.Name.ToUpper(),  dto.Remarks);
            await _dbContext.Locations.AddAsync(model);
            dto.Id = model.Id;
            return new ApiResponse<LocationDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<LocationDto> { Status = false, Message = e.Message };
        }
    }

    public async  Task<ApiResponse<LocationDto>> UpdateAsync(Guid id, LocationDto dto)
    {
        try
        {
            var model = await _repository.GetLocationByIdAsync(id);
            if (model == null) return new ApiResponse<LocationDto> { Status = false, Message = "更新数据失败" };
            model.Update(dto);
            return new ApiResponse<LocationDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<LocationDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<LocationDto>> DeleteAsync(Guid id)
    {
        try
        {
            var model = await _repository.GetLocationByIdAsync(id);
            if (model == null) return new ApiResponse<LocationDto> { Status = false, Message = "删除数据失败" };
            model.SoftDelete();//软删除
            return new ApiResponse<LocationDto> { Status = true };
        }
        catch (Exception e)
        {
            return new ApiResponse<LocationDto> { Status = false, Message = e.Message };
        }
    }
    #endregion

    #region 扩展查询
    public async Task<ApiResponse<LocationDto>> GetSingleByNameAsync(string name)
    {
        try
        {
            var model = await _repository.GetLocationByNameAsync(name);
            if (model == null) return new ApiResponse<LocationDto> { Status = false, Message = "查询数据失败" };
            var dto = _mapper.Map<LocationDto>(model);
            var cargo = await _repository.GetCargoByLocationAsync(dto.Name);
            if (cargo!=null)
            {
                dto.MtlNumber = cargo.MtlNumber;
                dto.Description = cargo.Description;
            }
            return new ApiResponse<LocationDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<LocationDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<IEnumerable<LocationDto>>> GetAllByAreaAsync(Warehouse_e warehouse, Area_e area, string ranks)
    {
        try
        {
            var models = await _repository.GetLocationsByAreaAsync(warehouse, area, ranks);
            //按照库位排序
            var dtos = await _mapper.ProjectTo<LocationDto>(models).OrderBy(x => x.Name).ToListAsync();
            //查询物料
            foreach (var dto in dtos)
            {
                var cargo = await _repository.GetCargoByLocationAsync(dto.Name);
                if (cargo==null) continue;
                dto.MtlNumber = cargo.MtlNumber;
                dto.Description = cargo.Description;
            }
            return new ApiResponse<IEnumerable<LocationDto>> { Status = true, Result = dtos };
        }
        catch (Exception e)
        {
            return new ApiResponse<IEnumerable<LocationDto>> { Status = false, Message = e.Message };
        }
    } 
    #endregion
}